home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / GNUST / !GNUst / st / TokenStrea < prev    next >
Text File  |  1991-09-13  |  3KB  |  119 lines

  1. "======================================================================
  2. |
  3. |   Token stream Method Definitions
  4. |
  5. |   A token stream is a stream that's defined in terms of a string.
  6. |   It basically parses off whitespace separated tokens as substrings
  7. |   and returns them (next).  If the entire contents of the string is 
  8. |   requested, it is returned as an Array containing the individual strings.
  9. |
  10.  ======================================================================"
  11.  
  12.  
  13. "======================================================================
  14. |
  15. | Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  16. | Written by Steve Byrne.
  17. |
  18. | This file is part of GNU Smalltalk.
  19. |
  20. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  21. | under the terms of the GNU General Public License as published by the Free
  22. | Software Foundation; either version 1, or (at your option) any later version.
  23. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  24. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  25. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  26. | details.
  27. | You should have received a copy of the GNU General Public License along with
  28. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  29. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  30. |
  31.  ======================================================================"
  32.  
  33.  
  34. "
  35. |     Change Log
  36. | ============================================================================
  37. | Author       Date       Change 
  38. | sbyrne     14 May 90      removed isWhiteSpace:;replaced uses with Character
  39. |              isSeparator.
  40. |
  41. | sbyrne     19 Sep 89      Changed to use real method categories.
  42. |
  43. | sbyrne     12 Jul 89      created.
  44. |
  45. "
  46.  
  47. Stream subclass: #TokenStream
  48.        instanceVariableNames: 'charStream'
  49.        classVariableNames: ''
  50.        poolDictionaries:''
  51.        category: nil.
  52.  
  53. TokenStream comment:
  54. 'I am not a typical part of the Smalltalk kernel class hierarchy.\n\
  55. I operate on a stream of characters and return distinct \n\
  56. (whitespace-delimited) groups of characters.' !
  57.  
  58.  
  59. !TokenStream class methodsFor: 'instance creation'!
  60.  
  61. on: aString
  62.     ^self new setStream: (ReadStream on: aString)
  63.  
  64. !!
  65.  
  66.  
  67.  
  68. !TokenStream methodsFor: 'basic'!
  69.  
  70. next
  71.     | char tokStream |
  72.     self atEnd ifTrue: [ ^nil ]. "has the nice side effect of skipping
  73.                                   leading white space."
  74.     tokStream _ WriteStream on: (String new: 1).
  75.     [ char _ charStream peek.
  76.       (char notNil) and: [ (char isSeparator) not ] ]
  77.         whileTrue: [ tokStream nextPut: (charStream next) ].
  78.     ^tokStream contents
  79. !
  80.  
  81. atEnd
  82.     | char |
  83.     [ char _ charStream peek.
  84.       char isNil ] whileFalse:
  85.         [ (char isSeparator) ifFalse: [ ^false ].
  86.      charStream next ].
  87.     ^true
  88. !
  89.  
  90. do: aBlock
  91.     [ self atEnd ] whileFalse:
  92.         [ aBlock value: self next ]
  93. !
  94.  
  95. contents
  96.     | arrayStream |
  97.     arrayStream _ WriteStream on: (Array new: 0).
  98.     self do: [ :aToken | arrayStream nextPut: aToken ].
  99.     ^arrayStream contents
  100. !!
  101.  
  102.  
  103.  
  104. !TokenStream methodsFor: 'write methods'!
  105.  
  106. nextPut: anObject
  107.     self shouldNotImplement
  108. !!
  109.  
  110.  
  111.  
  112. !TokenStream methodsFor: 'private'!
  113.  
  114. setStream: aStream
  115.     charStream _ aStream
  116. !!
  117.